package at.fhv.components.opening;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Customizer;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.media.jai.KernelJAI;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import at.fhv.framework.customizerTools.MatrixTablePanel;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * The DilateCustomizer class.
 * 
 * @author Michael Sieber
 */
public class OpeningCustomizer extends JPanel implements Customizer, ActionListener 
{
	private static final long serialVersionUID = -5576304645898860859L;
	
	private PropertyChangeSupport _pcsupport = new PropertyChangeSupport(this);
	private JButton _applyButton;
	private MatrixTablePanel _erodeMatrixPanel;
	private MatrixTablePanel _dilateMatrixPanel;
	private OpeningFilter _openingFilter;

	public OpeningCustomizer() 
	{
		initGUI();
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void setObject(Object obj) 
	{
		_openingFilter = (OpeningFilter) obj;
		
		KernelJAI erodeKernel = _openingFilter.getErodeKernel();
		KernelJAI dilateKernel = _openingFilter.getDilateKernel();
		
		_erodeMatrixPanel.setTableData(erodeKernel.getKernelData(), erodeKernel.getHeight(), erodeKernel.getWidth());
		_dilateMatrixPanel.setTableData(dilateKernel.getKernelData(), dilateKernel.getHeight(), dilateKernel.getWidth());
	}

	@Override
	public void addPropertyChangeListener(PropertyChangeListener pcl) 
	{
		_pcsupport.addPropertyChangeListener(pcl);
	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener pcl) 
	{
		_pcsupport.removePropertyChangeListener(pcl);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if (e.getSource().equals(_applyButton)) 
		{
			_openingFilter.setErodeKernel
			(
					new KernelJAI
					(
						_erodeMatrixPanel.getKernelHeight(),
						_erodeMatrixPanel.getKernelWidth(),
						_erodeMatrixPanel.translateKernel()
					)
			);
			
			_openingFilter.setDilateKernel
			(
					new KernelJAI
					(
						_dilateMatrixPanel.getKernelHeight(),
						_dilateMatrixPanel.getKernelWidth(),
						_dilateMatrixPanel.translateKernel()
					)
			);
		}
	}

	/**
	 * Initialize the gui
	 */
	private void initGUI() 
	{
		setLayout(new BorderLayout());
		_erodeMatrixPanel = new MatrixTablePanel();
		_dilateMatrixPanel = new MatrixTablePanel();
				
		//erode matrix panel
		JPanel erodeMatrixPanel = new JPanel(new BorderLayout());
		JPanel erodeLabelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		erodeLabelPanel.add(new JLabel("erode matrix"));
		erodeMatrixPanel.add(erodeLabelPanel, BorderLayout.NORTH);
		erodeMatrixPanel.add(_erodeMatrixPanel, BorderLayout.CENTER);
		
		//dilate matrix panel
		JPanel dilateMatrixPanel = new JPanel(new BorderLayout());
		JPanel dilateLabelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		dilateLabelPanel.add(new JLabel("dilate matrix"));
		dilateMatrixPanel.add(dilateLabelPanel, BorderLayout.NORTH);
		dilateMatrixPanel.add(_dilateMatrixPanel, BorderLayout.CENTER);
		
		_applyButton = new JButton("apply");
		_applyButton.addActionListener(this);
		
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(_applyButton);		
		
		add(erodeMatrixPanel, BorderLayout.WEST);
		add(dilateMatrixPanel, BorderLayout.EAST);
		add(buttonPanel, BorderLayout.SOUTH);
	}
}
